import java.io.*;
import java.nio.charset.StandardCharsets;

/**
 * @projectName: java-daily-questions
 * @package: PACKAGE_NAME
 * @className: FileDemo
 * @author: ZM
 * @description: io流
 * @date: 2021/8/18 10:01
 * @version: 1.0
 */
public class FileDemo {

    public static void copyToFile(String srcPath, String dstPath) throws IOException {
        File file = new File(srcPath);
        File dst = new File(dstPath);
        if (!file.exists()) {
            dst.createNewFile();
        }
        long length = file.length();
        FileInputStream fis = new FileInputStream(file);
        FileOutputStream fos = new FileOutputStream(dst);
        InputStreamReader isr = new InputStreamReader(fis, StandardCharsets.UTF_8);
        OutputStreamWriter osw = new OutputStreamWriter(fos, StandardCharsets.UTF_8);

        int count;
        char[] buffer = new char[30];
        while((count = isr.read(buffer)) != -1) {
            System.out.println(new String(buffer, 0, count));
            osw.write(buffer, 0, count);
        }
        System.out.println("文件大小：" + length +"Kb");
        System.out.println("文件名称：" + file.getName());
        System.out.println("文件路径：" + file.getPath());

        isr.close();
        osw.close();
    }

    public static void main(String[] args) throws IOException {
        String path = "D://a.txt";
        String dst = "D://b.txt";
        copyToFile(path, dst);
    }
}
